
#include "dialog.h"

#include <QApplication>

#include <QPainter>
#include <QPixmap>
#include <QDebug>

#include <QTime>
#include <QRandomGenerator>
#include <cstdlib>
#include <ctime>

#include <QThreadPool>

#include <QCoreApplication>
#include <QRunnable>>

inline void MyDrawTest(QPixmap *pixmap){
    std::srand(static_cast<unsigned>(std::time(0)));
    qDebug() << "thread";
    //            QPixmap pixmap(size());  // 创建与窗口大小相同的 QPixmap
    pixmap->fill(Qt::white);  // 用白色填充

    QPainter painter(pixmap);
    painter.setClipRect(QRect(0, 0, 50, 50));

    // 设置矩形的外框
    QPen pen(Qt::black, 2, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
    painter.setPen(pen);

    // 设置点阵填充的画刷
    QBrush brush(Qt::DiagCrossPattern);  // 采用点阵填充
    brush.setColor(Qt::blue);         // 设置填充颜色
    painter.setBrush(brush);

    // 绘制矩形
    QRect rectangle(50, 50, 200, 100);
    painter.drawRect(rectangle);


    for (int i = 0; i < 5000; ++i) {
        // 生成随机数
        int x = std::rand() % 100 + 1; // 生成1到100之间的整数
        int y = std::rand() % 100 + 1; // 生成1到100之间的整数

        // 绘制随机位置的矩形
        QRect rectanglex(x, y, 10, 10);
        painter.drawRect(rectanglex);
    }

};

class MyTask :public QObject, public QRunnable {
public:
    MyTask(QPixmap* pixmap) : m_pixmap(pixmap) {}

    void run() override {

        qDebug() << "PrintTask run 被调用，调用线程ID为：" << QThread::currentThread();

        qDebug() << "Task started with value:" << m_value;
        while (1){
            if (start){
                MyDrawTest(m_pixmap);
                start = false;
//                QMetaObject::invokeMethod(m_value, "requestMsg", Qt::QueuedConnection, Q_ARG(QString, msg));
            }
        }
        qDebug() << "Task completed with value:" << m_value;
    }

private:
    QLabel* m_value;
    QPixmap *m_pixmap;
    bool start{false};


public slots:
    void startDraw(QPixmap *pixmap){
        m_pixmap = pixmap;
        start = true;
    };
};


class DotMatrixRectangle : public QWidget {
public:
    DotMatrixRectangle(QWidget *parent = nullptr) : QWidget(parent) {
        this->setStyleSheet("background-color:white;");
        // 设置随机数种子
        std::srand(static_cast<unsigned>(std::time(0)));

        // 创建线程池
        threadPool = new QThreadPool;
        // 设置最大线程数，可以根据需要进行调整
        threadPool->setMaxThreadCount(10);
        std::vector<int> thread_flags = {};
        // 向线程池中添加任务
//        for (int i = 0; i < 10; ++i) {
//            MyTask *task = new MyTask(&thread_flags[i]);
//            threadPool->start(task);
//        }

    }
    QThreadPool *threadPool;

public slots:


protected:
    void paintEvent(QPaintEvent *) override {
//        QPainter painter(this);




        QPainter painter(this);

        std::vector<QPixmap> pixmap_arr = {};
         for (int i = 0; i < 20; i++) {
             QPixmap pixmap1(100, 100);
            // 创建线程并传递回调函数
            std::thread *th = new  std::thread(MyDrawTest, &pixmap1);
            th->join();
            painter.drawPixmap(i*100, 0, pixmap1);
//            pixmap_arr.push_back()
         }
         qDebug() << "RUN";




//        // 在窗口上绘制 QPixmap
//        QPainter widgetPainter(this);
//        widgetPainter.drawPixmap(0, 0, pixmap);
    }
};



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    Dialog w;
//    w.show();

    DotMatrixRectangle widget;
    widget.setGeometry(100, 100, 1300, 600);
    widget.show();
    return a.exec();
}
